package com.xuhuan.demo.excel.compare;

import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.xuhuan.demo.excel.compare.dto.ExcelLineDTO;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import org.apache.commons.collections.CollectionUtils;

/**
 * @author: huan.xu
 * @create: 2024/1/17 21:14
 * @description:
 **/
public class CopmareExcelDemo {

  /**
   * 新增的表
   */
  private static List<ExcelLineDTO> addTableNameList = new ArrayList<>();
  private static List<ExcelLineDTO> updateTableColumnList = new ArrayList<>();

  public static void main(String[] args) {
    String[] ignoreTableProfix = {"QRTZ_", "R_", "WF_", "MF_", "SDC_", "SIRM_", "UM_", "SPRT_"};
    String beforePath = "C:\\Users\\huan.xu\\Desktop\\表结构核对-20231123.xlsx";
    String afterPath = "C:\\Users\\huan.xu\\Desktop\\表结构核对-20240117.xlsx";
    Map<String, List<ExcelLineDTO>> beforeExcelDataMap = readGroupExcel(beforePath,
        ignoreTableProfix);
    Map<String, List<ExcelLineDTO>> afterExcelDataMap = readGroupExcel(afterPath,
        ignoreTableProfix);
    comareData(beforeExcelDataMap, afterExcelDataMap);

    // 比较完成，开始打印
    addTableNameList.stream()
        .forEach(excelLineDTO -> {
          System.out.println(
              excelLineDTO.getTableName() + "\t" + excelLineDTO.getColumnName() + "\t"
                  + excelLineDTO.getColumnComment());
        });
//    updateTableColumnList.stream()
//        .forEach(excelLineDTO -> {
//          System.out.println(
//              excelLineDTO.getTableName() + "\t" + excelLineDTO.getColumnName() + "\t"
//                  + excelLineDTO.getColumnComment());
//        });
  }

  private static void comareData(Map<String, List<ExcelLineDTO>> beforeMap,
      Map<String, List<ExcelLineDTO>> afterMap) {
    //如果后面的表名和表字段一样，才算没有改动
    for (String key : afterMap.keySet()) {
      // 判断新加的表
      List<ExcelLineDTO> beforeList = beforeMap.get(key);
      if (CollectionUtils.isEmpty(beforeList)) {
        List<ExcelLineDTO> afterList = afterMap.get(key);
        addTableNameList.addAll(afterList);
      } else {
        // 判断字段差异
        List<ExcelLineDTO> afterList = afterMap.get(key);

        List<ExcelLineDTO> updateList = afterList.stream()
            .filter(afterExcelLine -> !beforeList.contains(afterExcelLine))
            .collect(Collectors.toList());
        if (CollectionUtils.isNotEmpty(updateList)) {
          updateTableColumnList.addAll(updateList);
        }

      }
    }

  }

  private static Map<String, List<ExcelLineDTO>> readGroupExcel(String excelPath,
      String[] ignoreTableProfix) {
    List<ExcelLineDTO> excelLineDTOList = readExcel(excelPath);
    List<ExcelLineDTO> resultList = excelLineDTOList.stream()
        .filter(excelLineDTO -> {
          return !Arrays.asList(ignoreTableProfix)
              .contains(excelLineDTO.getTableName().split("_")[0] + "_");
        }).collect(Collectors.toList());
    return resultList.stream()
        .collect(Collectors.groupingBy(ExcelLineDTO::getTableName));
  }

  private static List<ExcelLineDTO> readExcel(String excelPath) {
    ExcelReader reader = ExcelUtil.getReader(excelPath);
    List<List<Object>> excelDataList = reader.read();

    List<ExcelLineDTO> collect = excelDataList.stream()
        .map(excelLine -> {
          String owner = (String) excelLine.get(0);
          String tableName = (String) excelLine.get(1);
          String columnName = (String) excelLine.get(2);
          String columnComment = (String) excelLine.get(3);
          return new ExcelLineDTO(owner, tableName, columnName, columnComment);
        })
        .filter(excelLineDTO -> {
          return !excelLineDTO.getOwner().equals("OWNER");
        })
        .collect(Collectors.toList());
    return collect;
  }
}
